<template>
  <div>
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item><a href="#">活动管理</a></el-breadcrumb-item>
      <el-breadcrumb-item>活动列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!--  输入框组-->
    <div>
      <br/>
      <span>活动名称 &nbsp;&nbsp;</span>
      <el-input
          placeholder="请输入活动名称"
          prefix-icon="el-icon-search"
          v-model="activity.name" class="inputMax" >
      </el-input>
      <span>&nbsp;&nbsp;活动地点&nbsp;&nbsp;</span>
      <el-input
          class="inputMax"
          placeholder="请输入活动地点"
          v-model="activity.place"
          prefix-icon="el-icon-location-information">
      </el-input>
      <span>&nbsp;&nbsp;活动类型&nbsp;&nbsp;</span>
      <el-select v-model="activity.type" placeholder="请选择" class="inputMax">
        <el-option
            v-for="item in activityTypes"
            :key="item.id"
            :label="item.name"
            :value="item.name">
        </el-option>
      </el-select>
      &nbsp;&nbsp;
      <el-button type="primary" icon="el-icon-search" @click="doActivitySearch" ><span>查询</span></el-button>
      <el-button type="success" icon="el-icon-plus" @click="toAddActivity">添加</el-button>
      <br/>
      <br/>
    </div>
    <el-table
        :data="activitys"
        border
        style="width: 100%">
      <el-table-column
          prop="id"
          label="ID"
          width="100">
      </el-table-column>
      <el-table-column
          prop="name"
          label="活动名称"
          width="100">
      </el-table-column>
      <el-table-column
          prop="place"
          label="活动地点"
          width="100">
      </el-table-column>
      <el-table-column
          prop="time"
          label="活动时间"
          width="100">
        <template slot-scope="scope">
          <span>{{scope.row.time | DateFormat}}</span>
        </template>
      </el-table-column>
      <el-table-column
          prop="type"
          label="活动类型"
          width="100">
      </el-table-column>
      <el-table-column
          prop="status"
          label="状态"
          width="80">
        <template slot-scope="scope">
          <span v-if="scope.row.status==0">未发布</span>
          <span v-else-if="scope.row.status==1">审核中</span>
          <span v-else>已发布</span>
        </template>
      </el-table-column>
      <el-table-column
          prop="content"
          show-overflow-tooltip
          label="活动内容">
      </el-table-column>
      <el-table-column
          fixed="right"
          width="270"
          label="操作">
        <template slot-scope="scope">
          <el-button type="success" icon="el-icon-check" size="mini" :disabled="scope.row.status!=0" @click="updateAct(scope.row)">发布</el-button>
          <el-button type="primary" icon="el-icon-edit" size="mini" :disabled="scope.row.status!=0" @click="doUpdateActivity(scope.row.id)" >编辑</el-button>
          <el-button type="danger" icon="el-icon-delete" size="mini" @click="open(scope.row.id)" >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
        background
        layout="prev, pager, next"
        :page-size="pageSize"
        :total="total">
    </el-pagination>
  </div>
</template>

<script>
import {activityDelete, activityList, activityTypeList,activityUpdate} from "@/api";

export default {
  name: "ActivityList",
  components: {
  },
  data() {
    return {
      activitys:[],
      activity:{
        id: 0,
        name: '',
        place: '',
        content: '',
        time: '',
        image: '',
        type: '',
        c_id: sessionStorage.getItem('cid'),
      },
      activityTypes: [],
      c_id: sessionStorage.getItem('cid'),
      total: 0,
      pageSize: 5,
    }
  },

  created() {
      activityTypeList().then((result)=>{
        this.activityTypes = result.data;
        this.activityTypes.unshift('');
      });

      activityList(this.activity).then((result)=>{
        this.activitys = result.data.list;
        this.total = result.data.total;
        this.pageSize = result.data.pagesize;
      })
  },

  methods: {
    toAddActivity(){
      this.$router.push('/mainCom/activityAdd');
    },

    doActivitySearch(){
      activityList(this.activity).then((result)=>{
        this.activitys = result.data.list;
        this.total = result.data.total;
        this.pageSize = result.data.pagesize;
      })
    },

    doUpdateActivity(id){
      this.$router.push({
        path: '/mainCom/activityAdd',
        query: {
          id: id,
        }
      })
    },
    updateAct(activity){
      activity.status=1;
      activityUpdate(activity);
    },
    open(id) {
      this.$confirm('此操作将永久删除该活动, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let activity = {
          id: id,
          c_id: this.c_id,
        }
        activityDelete(activity).then(()=>{
          let activity1 = {c_id: this.c_id};
          activityList(activity1).then((result)=>{
            this.activitys = result.data.list;
            this.total = result.data.total;
            this.pageSize = result.data.pagesize;
          });
        });
        this.$message({
          type: 'success',
          message: '删除成功!'
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
  }
}
</script>

<style scoped>
.inputMax{
  width: 200px;
}
</style>
